﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace DAL
{
    public class Where
    {
        public string ColName
        {
            get;
            set;
        }

        public OptType Opt
        {
            get;
            set;
        }

        public string ColValue
        {
            get;
            set;
        }

        public Where()
        {
        }

        public Where(string colname, OptType opt, string colvalue)
        {
            ColName = colname;
            Opt = opt;
            ColValue = colvalue;
        }

        public string GetOpt()
        {
            string result = "";
            switch (Opt)
            {
                case OptType.Equal:
                    result = "=";
                    break;
                case OptType.NotEqual:
                    result = "!=";
                    break;
                case OptType.Like:
                    result = " like ";
                    break;
                case OptType.NotLike:
                    result = " not like ";
                    break;
                case OptType.In:
                    result = " in ";
                    break;
                case OptType.NotIn:
                    result = " not in ";
                    break;
                case OptType.Great:
                    result = " > ";
                    break;
                case OptType.GreatThen:
                    result = " >= ";
                    break;
                case OptType.Less:
                    result = " < ";
                    break;
                case OptType.LessThen:
                    result = " <= ";
                    break;
            }

            return result;
        }

        public string GetSQL()
        {
            string result = string.Format(" and [{0}] {1} @{0} ", ColName, GetOpt());
            if (Opt == OptType.In || Opt == OptType.NotIn)
            {
                result = $" and [{ColName}] {GetOpt()}({ColValue}) ";
            }

            return result;
        }

        public SqlParameter GetParameter()
        {
            if (Opt == OptType.In || Opt == OptType.NotIn)
            {
                return null;
            }

            if (Opt == OptType.Like || Opt == OptType.NotLike)
            {
                return new SqlParameter(ColName, "%" + ColValue + "%");
            }

            return new SqlParameter(ColName, ColValue);
        }
    }
}
